package com.feige.service.impl;

import com.feige.helper.DeptHelper;
import com.feige.pojo.Dept;
import com.feige.dao.DeptMapper;
import com.feige.pojo.vo.BaseDeptVo;
import com.feige.service.DeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feige.utils.exception.CustomException;
import com.feige.utils.string.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author feige
 * @since 2020-10-23
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {

    @Override
    public List<Dept> queryAllDept() {
        List<Dept> deptList = this.baseMapper.selectList(null);
        return DeptHelper.build2(deptList);
    }

    @Override
    public List<BaseDeptVo> queryAllDeptTree(List<Long> ids) {
        List<BaseDeptVo> baseDeptVos = this.baseMapper.selectDept(ids);
        return DeptHelper.build1(baseDeptVos);
    }

    @Override
    public List<Long> queryDeptIds(Long id) {
        return this.baseMapper.queryDeptIds(id);
    }

    @Override
    public int saveDept(Dept dept) {
        if (dept.getPid() != 0){
            Dept parentDept = this.baseMapper.selectById(dept.getPid());
            if (parentDept.getStatus() == 0){
                throw new CustomException("部门停用，不许新增");
            }
            String newAncestors = StringUtils.isEmpty(parentDept.getAncestors()) ? String.valueOf(parentDept.getId()) : parentDept.getAncestors() + "," + parentDept.getId();
            dept.setAncestors(newAncestors);
        }
        return this.baseMapper.insert(dept);
    }

    @Override
    public int updateDeptById(Dept dept) {
        if (dept.getPid() != 0){
            Dept parentDept = this.baseMapper.selectById(dept.getPid());
            if (parentDept.getStatus() == 0){
                throw new CustomException("部门停用，不许新增");
            }
            Dept oldDept = this.baseMapper.selectById(dept.getId());
            // 不相等则说明该部门已换到其他部门，则需要把该部门下的所有子部门的祖级列表加上该部门的祖级列表
            if (!dept.getPid().equals(oldDept.getPid())){
                String newAncestors = StringUtils.isEmpty(parentDept.getAncestors()) ? String.valueOf(parentDept.getId()) : parentDept.getAncestors() + "," + parentDept.getId();
                String oldAncestors = oldDept.getAncestors();
                updateChildrenById(dept.getId(),newAncestors,oldAncestors);
                dept.setAncestors(newAncestors);
            }
        }else {
            dept.setAncestors("0");
        }
        return this.baseMapper.updateById(dept);
    }

    @Override
    public List<Dept> listDeptExcludeChild(Long id) {
        List<Dept> list = this.baseMapper.queryList();
        list.removeIf(dept -> dept.getId().intValue() == id ||
                ArrayUtils.contains(StringUtils.split(dept.getAncestors(), ","), id + ""));
        return list;
    }


    public void updateChildrenById(Long deptId, String newAncestors,String oldAncestors) {
        List<Dept> deptList = this.baseMapper.queryDepts(deptId);
        if (!deptList.isEmpty()){
            for (Dept dept : deptList) {
                String ancestors1 = dept.getAncestors().replace(oldAncestors,newAncestors);
                dept.setAncestors(ancestors1);
                updateById(dept);
            }
        }
    }
}
